package org.example;

public class Test1 {
    //leetcode 53 最大子数组和 https://leetcode.cn/problems/maximum-subarray/description/
    /**
     *dp
     * @param nums
     * @return
     */
    public int maxSubArray(int[] nums) {
        int n = nums.length, ret = nums[0];
        int prev = nums[0], cur = nums[0];
        for (int i = 1; i < n; i++) {
            cur = Math.max(prev+nums[i],nums[i]);
            prev = cur;
            ret = Math.max(ret,cur);
        }
        return ret;
    }

    //贪心
    public int maxSubArray1(int[] nums) {
        int sum = nums[0], ret = nums[0], n = nums.length;
        for (int i = 1; i < n; i++) {
            sum = Math.max(nums[i],sum + nums[i]);
            ret = Math.max(sum,ret);
        }
        return ret;
    }
}
